package com.hczx.caxa.util;

import com.hczx.caxa.bean.HttpResponseBean;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.impl.client.HttpClients;

import java.util.HashMap;
import java.util.Map;

public class HttpUtil {

    public static String getResponseContent(String url) {
        String bodyStr = StringUtils.EMPTY;
        try {
            Map<String, String> paramMap = new HashMap<>();
            HttpResponseBean response = doGetRequest(url, paramMap);
            if (response.getStatusCode() == HttpStatus.SC_OK) {
                bodyStr = IOUtils.toString(response.getBody(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bodyStr;
    }

    public static String getResponseContent(String url, Map<String, String> paramMap) {
        String bodyStr = StringUtils.EMPTY;
        try {
            HttpResponseBean response = doGetRequest(url, paramMap);
            if (response.getStatusCode() == HttpStatus.SC_OK) {
                bodyStr = IOUtils.toString(response.getBody(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bodyStr;
    }

    public static boolean isGetSuccess(String url, Map<String, String> paramMap) {
        HttpResponseBean response = doGetRequest(url, paramMap);
        return response.getStatusCode() == HttpStatus.SC_OK;
    }

    public static HttpResponseBean doGetRequest(String url) {
        HttpResponseBean response = new HttpResponseBean();
        try {
            HttpClient defaultClient = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(url);
            HttpResponse httpResponse = defaultClient.execute(new HttpGet(uriBuilder.build()));
            response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
            response.setBody(IOUtils.toByteArray(httpResponse.getEntity().getContent()));
            HttpClientUtils.closeQuietly(httpResponse);
            HttpClientUtils.closeQuietly(defaultClient);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static HttpResponseBean doPostRequest(String url) {
        HttpResponseBean response = new HttpResponseBean();
        try {
            HttpClient defaultClient = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(url);
            HttpResponse httpResponse = defaultClient.execute(new HttpPost(uriBuilder.build()));
            response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
            response.setBody(IOUtils.toByteArray(httpResponse.getEntity().getContent()));
            HttpClientUtils.closeQuietly(httpResponse);
            HttpClientUtils.closeQuietly(defaultClient);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static HttpResponseBean doGetRequest(String url, Map<String, String> paramMap) {
        HttpResponseBean response = new HttpResponseBean();
        try {
            HttpClient defaultClient = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(url);
            paramMap.forEach((key, value) -> uriBuilder.addParameter(key, value));
            HttpResponse httpResponse = defaultClient.execute(new HttpGet(uriBuilder.build()));
            response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
            response.setBody(IOUtils.toByteArray(httpResponse.getEntity().getContent()));
            HttpClientUtils.closeQuietly(httpResponse);
            HttpClientUtils.closeQuietly(defaultClient);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static HttpResponseBean doPostRequest(String url, Map<String, String> paramMap) {
        HttpResponseBean response = new HttpResponseBean();
        try {
            HttpClient defaultClient = HttpClients.createDefault();
            URIBuilder uriBuilder = new URIBuilder(url);
            paramMap.forEach((key, value) -> uriBuilder.addParameter(key, value));
            HttpResponse httpResponse = defaultClient.execute(new HttpPost(uriBuilder.build()));
            response.setStatusCode(httpResponse.getStatusLine().getStatusCode());
            response.setBody(IOUtils.toByteArray(httpResponse.getEntity().getContent()));
            HttpClientUtils.closeQuietly(httpResponse);
            HttpClientUtils.closeQuietly(defaultClient);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return response;
    }

    public static String getPostResponse(String url, Map<String, String> paramMap) {
        String bodyStr = StringUtils.EMPTY;
        try {
            HttpResponseBean response = doPostRequest(url, paramMap);
            if (response.getStatusCode() == HttpStatus.SC_OK) {
                bodyStr = IOUtils.toString(response.getBody(), "UTF-8");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return bodyStr;
    }


}
